package com.cavy.platform.server.service;

import com.cavy.platform.server.domain.PluginRepositorySetting;
import com.cavy.platform.server.dto.PluginRepositorySettingCreateDto;
import com.cavy.platform.server.dto.PluginRepositorySettingDeleteDto;
import com.cavy.platform.server.dto.PluginRepositorySettingDto;
import com.cavy.platform.server.mapper.PluginRepositorySettingMapper;
import com.cavy.platform.server.repository.PluginRepositorySettingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tangxu on 18-7-11.
 */
@Service
public class PluginRepositorySettingService {

    @Autowired
    private PluginRepositorySettingRepository repository;

    public void create(PluginRepositorySettingCreateDto dto) throws IOException {
        repository.insert(PluginRepositorySettingMapper.INSTANCE.convert(dto));
    }

    public void delete(PluginRepositorySettingDeleteDto dto) {
        repository.delete(dto.getName());
    }

    public List<PluginRepositorySettingDto> list() {
        List<PluginRepositorySetting> list = repository.getList();
        List<PluginRepositorySettingDto> settingDtos = new ArrayList<>(list.size());
        for (PluginRepositorySetting setting : list) {
            settingDtos.add(PluginRepositorySettingMapper.INSTANCE.convert(setting));
        }
        return settingDtos;
    }
}
